package com.glorysoft.wangnianxinpda.activity;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.glorysoft.wangnianxinpda.R;
import com.glorysoft.wangnianxinpda.base.App;
import com.glorysoft.wangnianxinpda.base.BaseActivity;
import com.glorysoft.wangnianxinpda.base.Constant;
import com.glorysoft.wangnianxinpda.databinding.ActivityMaterialOnScanBinding;
import com.glorysoft.wangnianxinpda.entity.MaterialInfoEntity;
import com.glorysoft.wangnianxinpda.entity.PDACurrentMtReturnEntity;
import com.glorysoft.wangnianxinpda.entity.net.ResponseEntity;
import com.glorysoft.wangnianxinpda.utils.GsonUtils;
import com.glorysoft.wangnianxinpda.utils.MyUtils;
import com.glorysoft.wangnianxinpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

public class MaterialOnScanActivity extends BaseActivity {
    private Context mContext;

    private String sEqpID = "";//设备号
    private String sSn = "";//物料条码
    private String sLotId = "";//工单批次号
    private String sMatLotId = "";//物料批次号

    private ActivityMaterialOnScanBinding binding = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutResId() {
        return 0;
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_material_on_scan);

        mContext = this;

        binding.etSn.requestFocus();

    }

    @Override
    protected void initData() {
        sEqpID = getIntent().getStringExtra("EQPID");
        binding.tvCurrentEqpNo.setText(sEqpID);

        MyUtils.setEditTextListener(binding.etSn, mContext, s -> {
            if (s.contains(";")){
                extracted(s,binding.etSn);
            }
        });

        MyUtils.setEditTextListener(binding.etQTY, mContext, s -> {
            if (s.contains(";")){
                extracted(s,binding.etQTY);
            }
        });

        MyUtils.setEditTextListener(binding.etMatLotId, mContext, s -> {
            if (s.contains(";")){
                extracted(s,binding.etMatLotId);
            }
        });

        MyUtils.setEditTextListener(binding.etTime, mContext, s -> {
            if (s.contains(";")){
                extracted(s,binding.etTime);
            }
        });

        MyUtils.setEditTextListener(binding.etEndTime, mContext, s -> {
            if (s.contains(";")){
                extracted(s,binding.etEndTime);
            }
        });

//        MyUtils.setEditTextListener(binding.etMatLotId, this, new FreeUI_TextCallback() {
//            @Override
//            public void onSuccess(String s) {
//                FreeUI_EntityCallBack<ResponseEntity<PDACurrentMtReturnEntity>> callBack = new FreeUI_DialogEntityCallBack<ResponseEntity<PDACurrentMtReturnEntity>>(new TypeToken<ResponseEntity<PDACurrentMtReturnEntity>>() {
//                }.getType(), getSupportFragmentManager(), this) {
//                    @Override
//                    public void onSuccess(Response<ResponseEntity<PDACurrentMtReturnEntity>> response) {
//                        super.onSuccess(response);
//                        if (response.body().isSuccess(false, getSupportFragmentManager())) {
//                                binding.etQTY.setText(response.body().getData().getUSERQTY());
//                        }
//                    }
//
//                    @Override
//                    public void onError(Response<ResponseEntity<PDACurrentMtReturnEntity>> response) {
//                        super.onError(response);
//                        loadError(response.getException(), "PDAGetMatInfo");
//                    }
//                };
//
//                JsonObject map = new JsonObject();
//                map.addProperty("WORK_NO", Constant.loginEntity.getWORK_NO());//工号
//                map.addProperty("EQPID", sEqpID);//设备号
//                map.addProperty("MLOTID", s);//物料批号
//                String method = "PDAGetMatInfo";
//                NetUtils.requestPostNet("PDAGetMatInfoRequest", method, this, map, callBack);
//            }
//        });
    }

    private void extracted(String s, EditText et) {
        String[] split = s.split(";",-1);
        if (split.length != 5){
            showToast("当前扫描信息有误，请重新扫描",false);
            binding.etSn.setText("");
            return;
        }

        binding.etSn.setText(split[0]);
        binding.etMatLotId.setText(split[1]);
        binding.etQTY.setText(split[2]);
        binding.etTime.setText(split[3]);
        binding.etEndTime.setText(split[4]);
        new Handler().postDelayed(() -> et.requestFocus(),500);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle(getString(R.string.materialOn));
        freeUI_titleView.setRightListener(getString(R.string.onMat), v -> {
            if (binding.etSn.getText().toString().isEmpty()){
                showToast(getString(R.string.please_scan_material_sn), false);
                return;
            }
//            if (sLotId.equals("")){
//                showToast(getString(R.string.please_scan_lot), false);
//                return;
//            }
            OnEQPMat();//上料
        });
        return new FreeUI_AddViewEntity("header", binding.llMain);
    }

    //region 接口调用

    //上料
    private void OnEQPMat() {
        FreeUI_EntityCallBack<ResponseEntity<?>> callBack = new FreeUI_DialogEntityCallBack<ResponseEntity<?>>(new TypeToken<ResponseEntity<?>>() {
        }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseEntity<?>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(false, getSupportFragmentManager())) {
                    if (!TextUtils.isEmpty(response.body().getMsg())) {
//                        finish();
                        showMessageDialog(response.body().getMsg(), getSupportFragmentManager());

                    }
                }
            }

            @Override
            public void onError(Response<ResponseEntity<?>> response) {
                super.onError(response);
                loadError(response.getException(), "PDAONEQPMat");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("WORK_NO", Constant.loginEntity.getWORK_NO());//工号
        map.addProperty("EQPID", sEqpID);//设备号
        map.addProperty("MATID", binding.etSn.getText().toString().trim());//物料编号
        map.addProperty("MATLOT", binding.etMatLotId.getText().toString().trim());//物料批号
//        map.addProperty("LOTID", binding.etLotId.getText().toString().trim());
        map.addProperty("DATE", binding.etTime.getText().toString().trim());
        map.addProperty("OUTDATE", binding.etEndTime.getText().toString().trim());
        map.addProperty("USERQTY", binding.etQTY.getText().toString().trim());
        String method = "PDAONEQPMat";
        NetUtils.requestPostNet("PDAONEQPMatRequest", method, this, map, callBack);
    }

    //endregion

    //消息提示框
    private void showMessageDialog(String message, FragmentManager fragmentManager) {
        FreeApi_DialogUtils.getConfirmDialog(
                App.app, "确定", message, this::finish
        ).show(fragmentManager, "DIALOG");

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

}